package web.config;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import web.filter.CheckTokenFilter;

import java.util.ArrayList;

/**
 * prePostEnabled 开启后,才能验证权限
 */
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    //创建BCryptPasswordEncoder注入容器
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }
    @Autowired
    private CheckTokenFilter checkTokenFilter;


    @Autowired
    private AuthenticationEntryPoint authenticationEntryPoint;

    @Autowired
    private AccessDeniedHandler accessDeniedHandler;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                //关闭csrf
                .csrf().disable()
                //关闭Session获取SecurityContext
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                // anonymous 只能未登录时访问,已登录不能访问
                // permitAll 未登录 + 登录 都能访问
                // anyRequest().authenticated 任意用户登录后,都能访问
                .antMatchers("/userlogin").anonymous()
//                .antMatchers("/userlogin").permitAll()
//                .antMatchers("/mysql").anonymous()
//                .antMatchers("/testCors").hasAuthority("system:dept:list222")
                // 除上面外的所有请求全部需要鉴权认证

//                .antMatchers("/**").anonymous() // 对所有接口不鉴权(测试业务用,如果controller上写了权限这个配置将失效)
                .anyRequest().authenticated();

        //在 Security 中添加token过滤器
        http.addFilterBefore(checkTokenFilter, UsernamePasswordAuthenticationFilter.class);

        //配置异常处理器
        http.exceptionHandling()
                //配置认证失败处理器
                .authenticationEntryPoint(authenticationEntryPoint)
                .accessDeniedHandler(accessDeniedHandler);

        //允许跨域
//        http.cors();
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

//    BCryptPasswordEncoder 验证
    public static void main(String[] args) {
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode("222");
        System.out.println(encode);

        String ss = "$2a$10$qWFI87o6NT6yDdlbL3YSJeKrl.5izrW7TchBzfW/S4LHt3BzljHNS";
        System.out.println(bCryptPasswordEncoder.matches("1",ss));
        System.out.println(bCryptPasswordEncoder.matches("2",ss));

        ArrayList<String> s = new ArrayList<>();
        s.add("333");
        s.add("444");
        s.add("555");
        s.add("666");
        s.add("777");
        s.add("888");
        for (String o :s) {
            System.out.println(o);
            System.out.println(bCryptPasswordEncoder.encode(o));
            System.out.println("---------------");
        }


    }
}
